#include "rkmedia_module_function.h"
#include "rkmedia_assignment_manage.h"
#include "rkmedia_config_public.h"
#include "rkmedia_container.h"

int init_rkmedia_module_function()
{

    rk_aiq_working_mode_t hdr_mode = RK_AIQ_WORKING_MODE_NORMAL;
    RK_BOOL bMultictx = RK_FALSE;
    RK_S32 s32CamId = 0;
    RK_U32 fps = 30;
    char *iq_file_dir = "/etc/iqfiles";
    int video_width = 1920;
    int video_height = 1080;   

    //开启ISP，否则获取RK_MPI_SYS_GetMediaBuffer 停止不前
    SAMPLE_COMM_ISP_Init(s32CamId, hdr_mode, bMultictx, iq_file_dir);
    SAMPLE_COMM_ISP_Run(s32CamId);
    SAMPLE_COMM_ISP_SetFrameRate(s32CamId, fps);

    // rkmedia_function_init();
    RK_MPI_SYS_Init();

    //视频输入配置
    RV1126_VI_CONFIG rkmedia_vi_config;
    memset(&rkmedia_vi_config, 0, sizeof(rkmedia_vi_config));
    rkmedia_vi_config.id = 0;
    rkmedia_vi_config.attr.pcVideoNode = CMOS_DEVICE_NAME;//VIDEO视频节点路径,
    rkmedia_vi_config.attr.u32BufCnt = 3;//VI捕获视频缓冲区计数，默认是3
    // rkmedia_vi_config.attr.u32Width = 2592;//视频输入的宽度，一般和CMOS摄像头或者外设的宽度一致
    // rkmedia_vi_config.attr.u32Height = 1944;//视频输入的高度，一般和CMOS摄像头或者外设的高度一致
    rkmedia_vi_config.attr.u32Width = video_width;//视频输入的宽度，一般和CMOS摄像头或者外设的宽度一致
    rkmedia_vi_config.attr.u32Height = video_height;//视频输入的高度，一般和CMOS摄像头或者外设的高度一致
    rkmedia_vi_config.attr.enPixFmt = IMAGE_TYPE_NV12;//视频输入的图像格式，默认是NV12(IMAGE_TYPE_NV12)
    rkmedia_vi_config.attr.enBufType = VI_CHN_BUF_TYPE_MMAP;//VI捕捉视频的类型
    rkmedia_vi_config.attr.enWorkMode = VI_WORK_MODE_NORMAL;//VI的工作模式，默认是NORMAL(VI_WORK_MODE_NORMAL)
    // int ret = rkmedia_vi_init(&rkmedia_vi_config); //初始化VI工作
    int ret = RK_MPI_VI_SetChnAttr(CAMERA_ID, rkmedia_vi_config.id, &rkmedia_vi_config.attr);
    //使能VI模块
    ret |= RK_MPI_VI_EnableChn(CAMERA_ID, rkmedia_vi_config.id);
    if (ret != 0)
    {
        printf("vi init error\n");
    }
    else
    {
        printf("vi init success\n");
        RV1126_VI_CONTAINTER vi_container;
        vi_container.id = 0;
        vi_container.vi_id = rkmedia_vi_config.id;
        set_vi_container(0, &vi_container);  //设置VI容器
    }

    //RGA配置
    RV1126_RGA_CONFIG rkmedia_rga_config = {0};
    memset(&rkmedia_rga_config, 0, sizeof(rkmedia_rga_config));
    rkmedia_rga_config.id = 0;
    rkmedia_rga_config.attr.bEnBufPool = RK_TRUE;
    rkmedia_rga_config.attr.u16BufPoolCnt = 4;
    rkmedia_rga_config.attr.u16Rotaion = 0;
    rkmedia_rga_config.attr.stImgIn.u32X = 0;
    rkmedia_rga_config.attr.stImgIn.u32Y = 0;
    rkmedia_rga_config.attr.stImgIn.imgType = IMAGE_TYPE_NV12;
    rkmedia_rga_config.attr.stImgIn.u32Width = video_width;
    rkmedia_rga_config.attr.stImgIn.u32Height = video_height;
    rkmedia_rga_config.attr.stImgIn.u32HorStride = video_width;
    rkmedia_rga_config.attr.stImgIn.u32VirStride = video_height;
    rkmedia_rga_config.attr.stImgOut.u32X = 0;
    rkmedia_rga_config.attr.stImgOut.u32Y = 0;
    rkmedia_rga_config.attr.stImgOut.imgType = IMAGE_TYPE_NV12;
    rkmedia_rga_config.attr.stImgOut.u32Width = video_width;
    rkmedia_rga_config.attr.stImgOut.u32Height = video_height;
    rkmedia_rga_config.attr.stImgOut.u32HorStride = video_width;
    rkmedia_rga_config.attr.stImgOut.u32VirStride = video_height;
    ret = RK_MPI_RGA_CreateChn(rkmedia_rga_config.id, &rkmedia_rga_config.attr); 
    if(ret != 0)
    {
        printf("RAG init error\n");
    }
    else
    {
        RV1126_RGA_CONTAINER rga_container;
        rga_container.id = 0;
        rga_container.rga_id = rkmedia_rga_config.id;
        set_rga_container(0, &rga_container); //设置VENC容器
        printf("rga init success\n");
    }

    //视频编码配置
    RV1126_VENC_CONFIG rkmedia_venc_config = {0};
    memset(&rkmedia_venc_config, 0, sizeof(rkmedia_venc_config));
    rkmedia_venc_config.id = 0;
    rkmedia_venc_config.attr.stVencAttr.enType = RK_CODEC_TYPE_H264;  //编码器协议类型
    rkmedia_venc_config.attr.stRcAttr.enRcMode = VENC_RC_MODE_H264CBR; //编码器码率控制模式
    rkmedia_venc_config.attr.stRcAttr.stH264Cbr.u32Gop = 30;            //GOPSIZE:关键帧间隔
    rkmedia_venc_config.attr.stRcAttr.stH264Cbr.u32BitRate = 1920 * 1080;//码率
    rkmedia_venc_config.attr.stRcAttr.stH264Cbr.fr32DstFrameRateDen = 1;  //目的帧率分子
    rkmedia_venc_config.attr.stRcAttr.stH264Cbr.fr32DstFrameRateNum = 30;//目的帧率分母
    rkmedia_venc_config.attr.stRcAttr.stH264Cbr.u32SrcFrameRateDen = 1; //源头帧率分子
    rkmedia_venc_config.attr.stRcAttr.stH264Cbr.u32SrcFrameRateNum = 30; //源头帧率分母
    rkmedia_venc_config.attr.stVencAttr.imageType = IMAGE_TYPE_NV12; //输入图像类型
    rkmedia_venc_config.attr.stVencAttr.u32PicWidth = video_width; //编码图像宽度
    rkmedia_venc_config.attr.stVencAttr.u32PicHeight = video_height;//编码图像高度
    rkmedia_venc_config.attr.stVencAttr.u32VirWidth = video_width;//编码图像虚宽度，一般来说u32VirWidth和u32PicWidth是一致的
    rkmedia_venc_config.attr.stVencAttr.u32VirHeight = video_height;//编码图像虚高度，一般来说u32VirHeight和u32PicHeight是一致的
    rkmedia_venc_config.attr.stVencAttr.u32Profile = 77;//编码等级H.264: 66: Baseline; 77:Main Profile; 100:High Profile; H.265: default:Main; Jpege/MJpege: default:Baseline(编码等级的作用主要是改变画面质量，66的画面质量最差利于网络传输，100的质量最好)
    ret = RK_MPI_VENC_CreateChn(rkmedia_venc_config.id, &rkmedia_venc_config.attr); 
    if(ret != 0)
    {
        printf("venc init error\n");
    }
    else
    {
        RV1126_VENC_CONTAINER venc_container;
        venc_container.id = 0;
        venc_container.venc_id = rkmedia_venc_config.id;
        set_venc_container(0, &venc_container); //设置VENC容器
        printf("venc init success\n");
    }


    //音频输入配置，参考官方示例rkmedia_ai_aenc_test.c
    RV1126_AI_CONFIG rkmedia_ai_config = {0}; 
    memset(&rkmedia_ai_config, 0, sizeof(rkmedia_ai_config));
    // rkmedia_ai_config.id = 0;
    rkmedia_ai_config.id = 0;
    rkmedia_ai_config.attr.pcAudioNode = AUDIO_PATH;  //音频alsa路径
    // rkmedia_ai_config.attr.enSampleFormat = RK_SAMPLE_FMT_S16; //音频采样格式,mp2
    rkmedia_ai_config.attr.enSampleFormat = RK_SAMPLE_FMT_FLTP; //音频采样格式,acc
    rkmedia_ai_config.attr.enAiLayout = AI_LAYOUT_NORMAL; //音频布局
    rkmedia_ai_config.attr.u32NbSamples = NB_SAMPLES; //采样个数，AAC默认1024
    // rkmedia_ai_config.attr.u32NbSamples = 1152; //采样个数,mp2
    // rkmedia_ai_config.attr.u32SampleRate = 48000; //音频采样率
    rkmedia_ai_config.attr.u32SampleRate = 16000; //音频采样率
    rkmedia_ai_config.attr.u32Channels = 2; //音频通道
    // ret = rkmedia_ai_init(&rkmedia_ai_config);
    ret = RK_MPI_AI_SetChnAttr(rkmedia_ai_config.id, &rkmedia_ai_config.attr); //设置AI属性
    ret |= RK_MPI_AI_EnableChn(rkmedia_ai_config.id); //使能AI模块
    if (ret != 0)
    {
        printf("ai init error\n");
    }
    else
    {
        printf("ai init success\n");
        RV1126_AI_CONTAINTER ai_container;
        ai_container.id = 0;
        ai_container.ai_id = rkmedia_ai_config.id;
        set_ai_container(0, &ai_container);  //设置AI容器
    }

    //音频编码配置. 不支持aac编码，舍弃，直接用ffmpeg把pcm转为acc
    RV1126_AENC_CONFIG rkmedia_aenc_config; 
    rkmedia_aenc_config.id = 0;
    // rkmedia_aenc_config.attr.enCodecType = RK_CODEC_TYPE_AAC; //AENC的编码类型
    rkmedia_aenc_config.attr.enCodecType = RK_CODEC_TYPE_MP2; //AENC的编码类型
    // rkmedia_aenc_config.attr.u32Bitrate = 153600; //AENC的音频码率
    rkmedia_aenc_config.attr.u32Bitrate = 64000; //AENC的音频码率
    rkmedia_aenc_config.attr.u32Quality = 1; //AENC编码质量
    // rkmedia_aenc_config.attr.stAencAAC.u32Channels = 2; //AENC编码通道
    // rkmedia_aenc_config.attr.stAencAAC.u32SampleRate = 48000; //AENC编码采样率
    rkmedia_aenc_config.attr.stAencMP3.u32Channels = 2; //AENC编码通道
    rkmedia_aenc_config.attr.stAencMP3.u32SampleRate = 16000; //AENC编码采样率
    // rkmedia_aenc_config.attr.stAencMP3.u32SampleRate = 44100; //AENC编码采样率
    // ret = rkmedia_aenc_init(&rkmedia_aenc_config);
    // if (ret != 0)
    // {
    //     printf("aenc init error\n");
    // }
    // else
    {
        RV1126_AENC_CONTAINER aenc_container;
        aenc_container.id = 0;
        aenc_container.aenc_id = rkmedia_aenc_config.id;
        set_aenc_container(0, &aenc_container);  //设置AENC容器
        printf("aenc init success\n");
    }


    return 0;
}
